package chapter_19


// 19.1 queues函数式队列

class Queue[T] (
  private val leading: List[T],
  private val trailing: List[T]
  ) {
  private def mirror =
    if (leading.isEmpty)
      new Queue(trailing.reverse, Nil)
    else this

  def head = mirror.leading.head

  def tail = {
    val q = mirror
    new Queue(q.leading.tail, q.trailing)
  }

  def append(x: T) =
    new Queue(leading, x :: trailing)

  override def toString = {
    "leading: " + leading.mkString(" ") + "\n" +
    "trailing: " + trailing.mkString(" ") + "\n"
  }
}

object chapter_19 {

  def main(args: Array[String]): Unit = {
    val q = new Queue(List(1, 2), List(2, 3))
    println(q)
    val q2 = q append 4
    println(q2)
  }
}
